package com.example.file.service;

import com.example.file.entity.ImportExcelVo;
import com.itextpdf.text.pdf.*;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.text.SimpleDateFormat;

public class FileService {
    public static Boolean fillTemplate(ImportExcelVo vo,Integer i) {
        //模板文件
        String fileName = "D:/sendemailtool/excel/PdfDemo.pdf";
        try {
            PdfReader reader = new PdfReader(fileName);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PdfStamper ps = new PdfStamper(reader, bos);

            BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

            SimpleDateFormat sf1 =new SimpleDateFormat("yy");
            SimpleDateFormat sf2 =new SimpleDateFormat("MM");
            SimpleDateFormat sf3 =new SimpleDateFormat("dd");
            SimpleDateFormat sf4 =new SimpleDateFormat("yyyy");
            SimpleDateFormat sf5 =new SimpleDateFormat("yyyy-MM-dd");
            String year = sf1.format(vo.getDate());
            String month = sf2.format(vo.getDate());
            String day = sf3.format(vo.getDate());
            String yearall = sf4.format(vo.getDate());
            String date = sf5.format(vo.getDate());


            AcroFields s = ps.getAcroFields();
            s.setFieldProperty("name", "textfont", bf, null);
            s.setFieldProperty("year", "textfont", bf, null);
            s.setFieldProperty("yearall", "textfont", bf, null);
            s.setFieldProperty("date", "textfont", bf, null);
            s.setFieldProperty("month", "textfont", bf, null);
            s.setFieldProperty("day", "textfont", bf, null);
            s.setFieldProperty("money1", "textfont", bf, null);
            s.setFieldProperty("money2", "textfont", bf, null);
            s.setFieldProperty("money3", "textfont", bf, null);
            s.setFieldProperty("money4", "textfont", bf, null);

            s.setField("name", vo.getName());
            s.setField("year", year);
            s.setField("yearall", yearall);
            s.setField("date", date);
            s.setField("month", month);
            s.setField("day", day);
            s.setField("money1", vo.getMoney().toString());
            s.setField("money2",  vo.getMoney1().toString());
            s.setField("money3",  vo.getMoney2().toString());
            s.setField("money4",  vo.getMoney3().toString());
            //设置为true/false在点击生成的pdf文档的填充域时有区别，
            ps.setFormFlattening(true);
            ps.close();
            //最终生成的文件，
            FileOutputStream fos = new FileOutputStream("D:/sendemailtool/excel/PdfDemoNew" +i+".pdf");
            fos.write(bos.toByteArray());
            fos.close();
            return true;


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
//        ImportExcelVo vo =new ImportExcelVo();
//        vo.setName("张三胖");
//        vo.setDate(new Date());
//        vo.setMoney(new BigDecimal("123451.12"));
//        vo.setMoney1(new BigDecimal("123451.12"));
//        vo.setMoney2(new BigDecimal("123451.12"));
//        vo.setMoney3(new BigDecimal("123451.12"));
//        fillTemplate(vo);

        System.out.println("--------------测试数据数据开始---------------");

    }
}
